﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace psafth.FSR.Lapcounter
{
    [Serializable]
    public class RaceCompetitor
    {
        private const int C_MAX_DECIMALS = 3;

        public double EndTime
        {
            get;
            set;
        }

        public int StartNumber
        {
            get;
            private set;
        }

        public string LicenceNumber
        {
            get;
            set;
        }

        public string FirstName
        {
            get;
            private set;
        }

        public string SurName
        {
            get;
            private set;
        }

        public List<Lap> Laps
        {
            get;
            set;
        }

        public ulong TransponderId
        {
            get;
            set;
        }

        public List<Transponder> Transponders
        {
            get;
            set;
        }

        public double BestLap
        {
            get
            {
                try
                {
                    if (Laps.Count(l => l.IsValid) > 0)
                    {
                        List<Lap> l = Laps.OrderBy(s => s.LapTime).ToList();

                        //Lap Best = Laps.FirstOrDefault(la => la.LapTime == l.Min(s => s.LapTime) && la.IsValid);

                        double bestLaptime = Laps.Where(la => la.IsValid).Min(la => la.LapTime);

                        if(bestLaptime > 0)
                            return bestLaptime;

                        return 0;
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch (Exception)
                {
#if DEBUG
                    throw;
#endif
                    return -2;
                }
            }
        }

        public Lap PreviousLap
        {
            get
            {
                try
                {
                    return Laps.Last(l => l.IsValid);
                }
                catch (Exception)
                {
                    return new Lap(0, DateTime.Now, 0, 0.0);
                }
            }
        }

        public double AverageLap()
        {

            double totalTime = 0.0;

            foreach (Lap l in Laps)
            {
                totalTime += l.LapTime;
            }

            return totalTime / Laps.Count(l => l.IsValid);

        }


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fName">Firstname of the competitor</param>
        /// <param name="lName">Lastname of the competitor</param>
        /// <param name="lNumber">Competitors national licencenumber</param>
        public RaceCompetitor(string fName, string lName, string lNumber, ulong tId, int sNumber)
        {
            this.FirstName = fName;
            this.SurName = lName;
            this.LicenceNumber = lNumber;
            this.TransponderId = tId;
            this.StartNumber = sNumber;
            this.Laps = new List<Lap>();
        }

        public RaceCompetitor(HeatDriver d)
        {
            this.FirstName = d.Driver.FirstName;
            this.SurName = d.Driver.LastName;
            this.LicenceNumber = d.Driver.LicenceNumber;
            this.Transponders = d.Driver.Transponders.ToList();
            this.StartNumber = d.StartNumber;
            this.Laps = new List<Lap>();

            ulong TranspId;
            if (string.IsNullOrEmpty(d.PreferredTransponder))
                d.PreferredTransponder = d.Driver.Transponders.First().ToString();

            ulong.TryParse(d.PreferredTransponder, out TranspId);
            this.TransponderId = TranspId;
        }
    }
}
